﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SerieInterface;
using MyInteractiveSerieList;
using System.Diagnostics;
using System.IO;

namespace SerieGUI
{
    public partial class MainWindow : Form
    {
        IHelping helping;
        /*#if DEBUG
            ClearAllPropertiesDebug clear = new ClearAllPropertiesDebug();
        #endif*/

        public MainWindow()
        {
            InitializeComponent();
            /*#if DEBUG
                clear.ClearAll();
            #endif*/
            helping = new Helping();
            helping.ListChanged += new ListEventHandler(helping_ListChanged);
            helping.SetImage += new SetImageEventHandler(helping_SetImage);
            helping.Update += new UpdateSeasonAndEpisodeEventHandler(helping_Update);
            helping.InitializeHelp();
        }

        void helping_Update(string serieInfo)
        {
            tbEpisode.Text = serieInfo.Substring(serieInfo.LastIndexOf("E") + 1);
            tbSeason.Text = serieInfo.Substring(serieInfo.LastIndexOf("S") + 1, 2);
        }

        void helping_SetImage(Image serieImage)
        {
            pbSeriePoster.Image = Properties.Resources.no_image;
            if (serieImage != null)
                pbSeriePoster.Image = serieImage;
        }

        string helping_ListChanged(List<ISerieInfo> listOfSeries)
        {
            cbSerieName.Items.Clear();
            cbSerieName.Items.Add("");
            cbSerieName.Items.Clear();
            foreach (ISerieInfo ser in listOfSeries)
            {
                if (ser.NextExist)
                    cbSerieName.Items.Add(ser);
            }
            if (cbSerieName.Items.Count > 0)
            {
                cbSerieName.Text = cbSerieName.Items[0].ToString();

                return cbSerieName.Text;
            }
            pbSeriePoster.Image = Properties.Resources.no_image;
            this.Text = "Watch a serie";
            return null;
        }

        private void cbSerieName_SelectedIndexChanged(object sender, EventArgs e)
        {
            helping.SerieChanged((ISerieInfo)cbSerieName.Items[cbSerieName.SelectedIndex]);
            this.Text = "Watch " + cbSerieName.Text;
        }

        #region Add or remove serie path
        private void addMoviePathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = "Test";
            if (InputBox("Add root folder for series", "Folder path:", ref value) == DialogResult.OK)
            {
                if (value[value.Length - 1] != '\\')
                    value = value + "\\";
                helping.AddPath(value);
            }
        }

        private void removeMoviePathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = "Test";
            if (InputBox("Remove root folder for series", "Folder path:", ref value) == DialogResult.OK)
            {
                if (value[value.Length - 1] != '\\')
                    value = value + "\\";
                helping.RemovePath(value);
            }
        }
        #endregion

        private void bNextEpisode_Click(object sender, EventArgs e)
        {
            string outEpisode, outSeason, serie = cbSerieName.SelectedItem.ToString();
            helping.WatchNextEpisode((ISerieInfo)cbSerieName.Items[cbSerieName.SelectedIndex], tbEpisode.Text, tbSeason.Text,
                out outEpisode, out outSeason);
            tbSeason.Text = outSeason;
            tbEpisode.Text = outEpisode;
            if (serie != cbSerieName.SelectedItem.ToString())
                helping.SerieChanged((SerieInfo)cbSerieName.SelectedItem);
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exportFile.InitialDirectory = "C:\\";
            exportFile.Filter = "Text Files (*.txt)|*.txt";
            if (exportFile.ShowDialog() == DialogResult.OK)
            {
                StreamWriter exportStream = new StreamWriter(exportFile.FileName, false);
                string exportText = helping.Exporting();
                foreach (string serieItem in exportText.Split(';'))
                {
                    exportStream.WriteLine(serieItem);
                }
                exportStream.Close();
            }
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            importFile.InitialDirectory = "C:\\";
            importFile.FileName = "LES";
            importFile.Filter = "Text Files (*.txt)|*.txt";
            if (importFile.ShowDialog() == DialogResult.OK)
            {
                StreamReader importStream = new StreamReader(importFile.FileName);
                string importText = importStream.ReadToEnd();
                importStream.Close();
                if (helping.Importing(importText))
                {
                    MessageBox.Show("The info in the imported file couldn't be parsed.\nCheck if it's " +
                        "the correct file and that the information is formated in the correct way.", "ERROR");
                }
            }
        }

        #region InputBox
        private static DialogResult InputBox(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();

            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }
        #endregion
    }
}
